package com.iot.inclinometer.util;

import android.content.Context;
import android.widget.Toast;

import com.iot.inclinometer.pojo.BaseInfo;
import com.iot.inclinometer.pojo.ObserveInfo;

import java.net.InetAddress;

/*
 * 工具类
 * */
public class BaseUtil {
	/*
	 * 检验钻孔基本信息是否有空值
	 * */
	public static boolean isEmpty(BaseInfo baseInfo,Context context){
		boolean flag = false;
		if (baseInfo.getProjectName()==null||"".equals(baseInfo.getProjectName())) {
			Toast.makeText(context, "工程名不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}
		else if(baseInfo.getItemName()==null|| "".equals(baseInfo.getItemName())){
			Toast.makeText(context, "工点名不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}
		else if(baseInfo.getDrillNum()==null|| "".equals((baseInfo.getDrillNum()))){
			Toast.makeText(context, "钻孔编号不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}
		else if(baseInfo.getDepth()==null|| "".equals(baseInfo.getDepth())){
			Toast.makeText(context, "孔深不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}
		else if((!"".equals(baseInfo.getMark2())&&!"".equals(baseInfo.getMark4()))||(!"".equals(baseInfo.getX())&&!"".equals(baseInfo.getY()))){
			//如果里程桩号不为空，或者，坐标不为空
			flag = true;
		}
		else {
			Toast.makeText(context, "里程桩号与坐标必须填写一个！", Toast.LENGTH_LONG).show();
			return flag;
		}

		return flag;
	}
	/*
	 * 检验观测基本信息是否有空值
	 * */
	public static boolean isEmpty(ObserveInfo observeInfo,Context context){
		boolean flag = false;
		if(observeInfo.getObserver()==null||"".equals(observeInfo.getObserver())){
			Toast.makeText(context, "观测人员不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}
		else if (observeInfo.getTime()==null||"".equals(observeInfo.getTime())) {
			Toast.makeText(context, "观测时间不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}
		else{
			flag = true;
		}
		return flag;
	}
	
	/*
	 * dip转为px
	 * */
	public static int dip2px(Context context, float dpValue)  
	{  
	    final float scale = context.getResources().getDisplayMetrics().density;  
	    return (int) (dpValue * scale + 0.5f);  
	}
	
    /*
     * 检验配置参数是否有空值
     * */
	@SuppressWarnings("finally")
	public static boolean isEmpty(String[] configuration,Context context){
		boolean flag = false;
		
		if(configuration[0]==null||"".equals(configuration[0])){
			Toast.makeText(context, "A_X不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[1]==null||"".equals(configuration[1])){
			Toast.makeText(context, "A_Y不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[2]==null||"".equals(configuration[2])){
			Toast.makeText(context, "A_Z不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}if(configuration[3]==null||"".equals(configuration[3])){
			Toast.makeText(context, "B_X不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[4]==null||"".equals(configuration[4])){
			Toast.makeText(context, "B_Y不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[5]==null||"".equals(configuration[5])){
			Toast.makeText(context, "B_Z不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[6]==null||"".equals(configuration[6])){
			Toast.makeText(context, "Tclib不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[7]==null||"".equals(configuration[7])){
			Toast.makeText(context, "matA11不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[8]==null||"".equals(configuration[8])){
			Toast.makeText(context, "matA12不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[9]==null||"".equals(configuration[9])){
			Toast.makeText(context, "matA13不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[10]==null||"".equals(configuration[10])){
			Toast.makeText(context, "matA21不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[11]==null||"".equals(configuration[11])){
			Toast.makeText(context, "matA22不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[12]==null||"".equals(configuration[12])){
			Toast.makeText(context, "matA23不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[13]==null||"".equals(configuration[13])){
			Toast.makeText(context, "matA31不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[14]==null||"".equals(configuration[14])){
			Toast.makeText(context, "matA32不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
		}else if(configuration[15]==null||"".equals(configuration[15])){
			Toast.makeText(context, "matA33不能为空！", Toast.LENGTH_LONG).show();
			return flag;		
			
		}else if(configuration[16]==null||"".equals(configuration[16])){
			Toast.makeText(context, "dAX不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[17]==null||"".equals(configuration[17])){
			Toast.makeText(context, "dAY不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[18]==null||"".equals(configuration[18])){
			Toast.makeText(context, "dAZ不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[19]==null||"".equals(configuration[19])){
			Toast.makeText(context, "dBX不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[20]==null||"".equals(configuration[20])){
			Toast.makeText(context, "dBY不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[21]==null||"".equals(configuration[21])){
			Toast.makeText(context, "dBZ不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[22]==null||"".equals(configuration[22])){
			Toast.makeText(context, "产品型号不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[23]==null||"".equals(configuration[23])){
			Toast.makeText(context, "服务器的IP地址不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[24]==null||"".equals(configuration[24])){
			Toast.makeText(context, "端口号不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		} else if(configuration[25]==null||"".equals(configuration[25])){
			Toast.makeText(context, "每次测量读数组数不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else if(configuration[26]==null||"".equals(configuration[26])){
			Toast.makeText(context, "phiX不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else  if(configuration[27]==null||"".equals(configuration[27])){
			Toast.makeText(context, "方差值不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else  if(configuration[28]==null||"".equals(configuration[28])){
			Toast.makeText(context, "接口地址不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else{
			try {
				if(InetAddress.getByName(configuration[23])==null){	
					
				}else{
					if(Integer.parseInt(configuration[24])<0||Integer.parseInt(configuration[24])>65535){
						Toast.makeText(context, "端口号不合法！", Toast.LENGTH_LONG).show();
						return flag;
					}else{
						flag = true;
					}
				}				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Toast.makeText(context, "服务器的IP地址不合法！", Toast.LENGTH_LONG).show();
				e.printStackTrace();
				
			}finally{
				return flag;
			}
		}
	}
	/*
	 * 检验文件夹目录是否为空值
	 * */
	public static boolean isEmpty(String path,Context context){
		boolean flag = false;
		if(path==null||"".equals(path)){
			Toast.makeText(context, "文件夹目录不能为空！", Toast.LENGTH_LONG).show();
			return flag;
		}else{
			flag = true;
		}
		return flag;
	}
	
}
